package cn.iocoder.yudao.module.lowcode.dal.mysql.magicapi;

import cn.iocoder.yudao.framework.mybatis.core.mapper.BaseMapperX;
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
import cn.iocoder.yudao.module.lowcode.dal.dataobject.magicapi.MagicApiFileDO;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * Magic API 文件 Mapper
 *
 * @author 芋道源码
 */
@Mapper
public interface MagicApiFileMapper extends BaseMapperX<MagicApiFileDO> {

    /**
     * 批量插入 Magic API 文件
     */
    @Insert({
        "<script>",
        "INSERT INTO magic_api_file_v2 (file_path, file_content) VALUES",
        "<foreach collection='files' item='file' separator=','>",
        "(#{file.filePath}, #{file.fileContent})",
        "</foreach>",
        "ON DUPLICATE KEY UPDATE file_content = VALUES(file_content)",
        "</script>"
    })
    void insertBatch(@Param("files") List<MagicApiFileDO> files);

    /**
     * 根据路径前缀删除文件（删除整个分组）
     */
    @Delete("DELETE FROM magic_api_file_v2 WHERE file_path LIKE CONCAT(#{pathPrefix}, '%')")
    int deleteByPathPrefix(@Param("pathPrefix") String pathPrefix);

    /**
     * 根据确切路径删除文件
     */
    @Delete("DELETE FROM magic_api_file_v2 WHERE file_path = #{filePath}")
    int deleteByFilePath(@Param("filePath") String filePath);
}